package codigos;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.mail.MessagingException;
import org.primefaces.model.UploadedFile;

public class Subir_ficheros {
    public void upload(String destino, UploadedFile uf) throws IOException, MessagingException {
        
        File folder = new File(destino);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        copyFile(destino+ uf.getFileName(), uf.getInputstream()); 
    }  
    
    public void copyFile(String fileName, InputStream in) {
        try {
            OutputStream out = new FileOutputStream(new File(fileName));

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            in.close();
            out.flush();
            out.close();
            
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
    
            
}